const db= require('../../db/index');
// 获取文章列表 展示 筛选
let getlist=(req,res)=>{
    // 准备从哪里开始 找几个
    let start=(req.query.pagenum-1)*req.query.pagesize;
    let pagesize=req.query.pagesize;
    // sql语句找id 标题 时间 状态 分类名称 
    let sqlStr=`SELECT cle.id,cle.title,cle.pub_date,cle.state,cate.name AS cate_name
    from ev_articles AS cle 
    LEFT JOIN ev_article_cate AS cate 
    ON cle.cate_id=cate.Id 
    WHERE 1=1 `;
    let countSql=`select count(*) as total from ev_articles as cle where 1=1 `
    if(req.query.cate_id){
        sqlStr+=' and cle.cate_id='+req.query.cate_id; 
        countSql+=' and cle.cate_id='+req.query.cate_id
    }
    if(req.query.state){
        sqlStr+=` and cle.state='${req.query.state}'`
        countSql+=` and cle.state='${req.query.state}'`
    }
    sqlStr+=` limit ${start},${pagesize}`;
    // 执行sql
    db.query(sqlStr,(err,results)=>{
        console.log(results);
        if(err) return res.cc(err);
        db.query(countSql,(err,count)=>{
            if(err) return res.cc(err);
            res.send({
                status:0,
                message:'获取文章的列表数据成功',
                data:results,
                ...count[0]
            })
        })
    })
}
module.exports =getlist